﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGSVR.Common;
using LGSVR.BLL.Terminal;
using LGSVR.BLL.Sys;
using System.Data;

namespace LGSVR_WEB.terminal
{

    public partial class CardGroupEdit : DataBasePage
    {
        private int Pid
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["ID"]) ? 0 : int.Parse(Request.QueryString["ID"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                PageInit();
            }
        }
        void PageInit()
        {
            //绑定ParentID
            bool isSupperAdmin = Session["ActName"].ToString() == "超级管理员";
           
            //CardGroupBLL cardGroupBll = new CardGroupBLL();
            //dropParentID.DataSource = cardGroupBll.GetAllGroup(Pid, isSupperAdmin?"":Session["UserID"].ToString());
            //dropParentID.DataTextField = "NewGroupName";
            //dropParentID.DataValueField = "groupID";
            //dropParentID.DataBind();
            DataTable groupDt = GetListByTable("cardGroup", "state=1");
            string parentId = Request["parentID"];
            string strWhere = "";
            if (string.IsNullOrEmpty(parentId))
            {
                strWhere = " parentID is null or parentID=''";
            }
            else
            {
                strWhere = " groupID='" + parentId + "'";
            }
            foreach (DataRow dr in groupDt.Select(strWhere))
            {
                dropParentID.Items.Add(new ListItem(dr["groupName"].ToString(), dr["groupID"].ToString()));
            }

            
            dropConfigID.DataSource = GetListByTable("configInfo", "state=1");
            dropConfigID.DataTextField = "configID";
            dropConfigID.DataValueField = "configID";
            dropConfigID.DataBind();

            DataTable dtCompany = new CompanyBLL().GetCompanyList("");
            if (!isSupperAdmin)
            {
                var newcompany = from c in dtCompany.AsEnumerable()
                                 where c.Field<string>("companyID") == Session["CompanyID"].ToString()
                                 select new ListItem
                                 {
                                     Text = c.Field<string>("companyName"),
                                     Value = c.Field<string>("companyID")
                                 };
                dropCompanyID.Items.AddRange(newcompany.ToArray());
            }
            else
            {
                dropCompanyID.DataSource = dtCompany;
                dropCompanyID.DataTextField = "companyName";
                dropCompanyID.DataValueField = "companyID";
                dropCompanyID.DataBind();
            }


            Dictionary<string, string> dicSingle;
            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                btnSave.Text = "更 新";
                if (Utils.IsNumber(Request.QueryString["ID"]))
                {
                    dicSingle = GetSigle("cardGroup", "ID=" + Request.QueryString["ID"]);

                    if (dicSingle != null)
                    {
                        txtcontent.Text = dicSingle["content"];
                        txtGroupID.Text = dicSingle["groupID"];
                        txtGroupName.Text = dicSingle["groupName"];

                        dropConfigID.SelectedValue = dicSingle["configID"];
                        chkhasCard.Checked = Convert.ToBoolean(dicSingle["hasCard"]);
                        chkisUsed.Checked = Convert.ToBoolean(dicSingle["isUsed"]);                        
                        chkisUsed.Checked = Convert.ToBoolean(dicSingle["isUsed"]);
                        chkstate.Checked = Utils.ToBool(dicSingle["state"]);
                        dropCompanyID.SelectedValue = dicSingle["companyID"];
                        txtcontent.Text = dicSingle["content"];
                        dropParentID.SelectedValue = dicSingle["parentID"];
                    }
                    else
                    {
                        MsgContent = ShowMessage("数据加载失败。", MessageType.Error, true, false);
                    }
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {

            CardGroupBLL cardTypeBll = new CardGroupBLL();
            if (cardTypeBll.IsHasCardGroupID(txtGroupID.Text.Trim(), Pid))
            {
                MsgContent = ShowMessage("已存在该组别名称，请更换！", MessageType.Error, false, true);
                return;
            }
            Dictionary<string, string> dicParam = new Dictionary<string, string>();
            dicParam.Add("groupID", txtGroupID.Text.Trim());
            dicParam.Add("groupName", txtGroupName.Text.Trim());
            dicParam.Add("configID", dropConfigID.SelectedValue);
            dicParam.Add("isUsed", chkisUsed.Checked ? "1" : "0");
            dicParam.Add("hasCard", chkhasCard.Checked ? "1" : "0");
            dicParam.Add("state", chkstate.Checked ? "1" : "0");
            dicParam.Add("groupLevel", dropLevel.SelectedValue);
            dicParam.Add("companyID", dropCompanyID.SelectedValue);
            dicParam.Add("content", txtcontent.Text.Trim());
            dicParam.Add("parentID", dropParentID.SelectedValue);

            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                if (Utils.IsNumber(Request.QueryString["ID"]))
                {
                    Edit("cardGroup", dicParam, "ID=" + Pid);

                    if (Result > 0)
                    {
                        MsgContent = ShowMessage("恭喜您，更新成功！", MessageType.Success, false, true);
                        NoRefresh();
                    }
                    else
                    {
                        if (Result == -100)
                        {
                            MsgContent = ShowMessage("对不起，操作失败，程序参数异常。", MessageType.Error, false, true);
                        }
                        else
                        {
                            MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                        }
                    }
                }
            }
            else  // 新增
            {
                Insert("cardGroup", dicParam);

                if (Result > 0)
                {
                    MsgContent = ShowMessage("恭喜您，添加成功！", MessageType.Success, false, true);
                    NoRefresh();
                }
                else
                {
                    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                }
            }

        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["backUrlParam"]))
            {
                Response.Redirect("CardGroupList.aspx?" + Request.QueryString["backUrlParam"], false);
            }
            else
            {
                Response.Redirect("CardGroupList.aspx", false);
            }
        }
    }
}